/****************************** Module Header ******************************\
 * Module Name:  PageBasic.cs
 * Module Version:  1.0.0 (2011-9-30)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is a basic class for the webpage.  It supplies common public
 * methods to process a internet request, such as Request processing and so on. 
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;

namespace UCF.Core.Web
{
    /// <summary>
    /// 页面通用基类
    /// <para>版本：1.0.0 [2011-11-17]</para>
    /// <para>提供了页面消息对象、Request二次封装、获取客户端IP等通用功能</para>
    /// </summary>
    public class WebPageBase : System.Web.UI.Page
    {
        #region 类属性及字段               

        #region 数据库连接
        
        /// <summary>
        /// Web.Config文件中定义的数据库连接名称
        /// </summary>
        public static string ConnectionName { get; set; }

        /// <summary>
        /// 数据库连接信息,第一个是连接字符串，第二个是连接提供程序
        /// <para>格式为 [0]=ConnectionString,[1]=ProviderName</para>
        /// </summary>
        public static string[] ConnectionInfo = { "", "" };
        #endregion

        #region 站点相关路径
        
        /// <summary>
        /// 用户文件上传路径
        /// </summary>
        public static string ADFUserPath { get; set; }

        /// <summary>
        /// 应用程序数据文件路径(Applicaton Data File)
        /// </summary>
        public static string ADFPath { get; set; }

        /// <summary>
        /// 网站首页地址
        /// </summary>
        public string WebsiteURL { get; set; }

        /// <summary>
        /// 文件上传目录生成模式
        /// <para>0=按日期生成,1=按文件名md5生成</para>
        /// </summary>
        public static short UploadFolderGenerateMode { get; set; }

        /// <summary>
        /// 后台管理系统目录
        /// </summary>
        public static string WebAdminRoot { get; set; }
        #endregion

        /// <summary>
        /// 页面消息对象
        /// </summary>
        public Dictionary<string, string> PageMessage = new Dictionary<string, string>();
        #endregion

        /// <summary>
        /// 构造方法
        /// </summary>
        public WebPageBase() { }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="connectionName"></param>
        public WebPageBase(string connectionName)
        {
            ConnectionName = connectionName;
            ConnectionStringSettings connSetting = ConfigurationManager.ConnectionStrings[connectionName];
            if (connSetting == null) throw new Exception("Connection Not Define! Please Contact Administrator.");
            ConnectionInfo[0] = connSetting.ConnectionString;
            ConnectionInfo[1] = connSetting.ProviderName;
        }

        #region 实例方法
        /// <summary>
        /// 添加消息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="title"></param>
        public void AddPageMessage(string message, string title = "")
        {
            if (title.Length == 0) title = (this.PageMessage.Count + 1).ToString();
            this.PageMessage[title] = message;
        }

        /// <summary>
        /// 检测表单参数类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <param name="errTip"></param>
        /// <param name="minLength"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public bool CheckFormParameter<T>(string value, string errTip, int minLength = 1, int maxLength = 0)
        {
            return value.IsValidParameter<T>(ref this.PageMessage, errTip, minLength, maxLength);
        }

        /// <summary>
        /// 获取新的页面消息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="messagecontent"></param>
        /// <returns></returns>
        public string[] NewMessage(string key, string messagecontent)
        {
            return new string[] { key, messagecontent };
        }                
        #endregion
        
        

    }
}
